package com.uduemc.biso.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;

@Component
public class IPFilter extends ZuulFilter {

	private static final Logger logger = LoggerFactory.getLogger(IPFilter.class);

	@Override
	public boolean shouldFilter() {
		return true;
	}

	@Override
	public String filterType() {
		return "pre";
	}

	@Override
	public int filterOrder() {
		return 0;
	}

	@Override
	public Object run() throws ZuulException {
		RequestContext ctx = RequestContext.getCurrentContext();
		String ip = ctx.getRequest().getRemoteAddr();
		logger.info("access ip: " + ip);
		/**
		 * Set<String> blackList = ConcurrentCache.getBlackSet(); Set<String> whiteList
		 * = ConcurrentCache.getWhiteSet();
		 * 
		 * blackList.removeAll(whiteList);
		 * 
		 * // 在黑名单中禁用 if (StringUtils.isNotBlank(ip) && blackList.contains(ip)) {
		 * ctx.setSendZuulResponse(false); ctx.setResponseBody("Suspected flooding
		 * attack, IP blocked");
		 * ctx.setResponseStatusCode(HttpStatus.FORBIDDEN.value());
		 * ctx.addZuulResponseHeader(HttpHeaders.CONTENT_TYPE,
		 * MediaType.APPLICATION_JSON_UTF8_VALUE); return null; }
		 */
		return null;
	}
}
